package com.xiaotao.controller;

import com.nimbusds.jose.JOSEException;
import com.xiaotao.entity.Merchant;
import com.xiaotao.service.MerchantService;
import com.xiaotao.util.ResponseEnum;
import com.xiaotao.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Api(tags = "商户相关")
@RestController
@RequestMapping("merchant")
public class MerchantController {
    @Resource
    private MerchantService merchantService;

    //添加商户信息
    @ApiOperation(value = "添加商户")
    @PostMapping("addMerchant")
    public ResponseUtil Add(@RequestBody Merchant merchant) {
        return merchantService.save(merchant);
    }

    //商户的登录
    @ApiOperation(value = "商户登录")
    @PostMapping("login")
    public ResponseUtil login(@RequestBody Merchant merchant) throws JOSEException {
        return merchantService.login(merchant);
    }

    //查询所有商户
    @ApiOperation(value = "查询所有商户")
    @GetMapping("{pageNum}/{pageSize}")
    public ResponseUtil findAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        return merchantService.findAll(pageNum, pageSize);
    }

    //查询所有商户
    @ApiOperation(value = "查询所有商户")
    @GetMapping()
    public ResponseUtil findAll() {
        return merchantService.findAll2();
    }

    //删除商户
    @ApiOperation(value = "删除商户")
    @DeleteMapping("{id}")
    public ResponseUtil delete(@PathVariable Integer id) {
        return merchantService.delete(id);
    }

    //根据id查询商户
    @ApiOperation(value = "根据id查询商户")
    @GetMapping("/{id}")
    public ResponseUtil findById(@PathVariable Integer id) {
        return merchantService.findById(id);
    }

    //商户上传图片
    @ApiOperation(value = "商户图片上传")
    @PostMapping("/upload")
    public ResponseUtil uploadImage(MultipartFile file) throws Exception {
        return merchantService.uploadImage(file, "merchant");
    }

    //条件查询
    @ApiOperation(value = "关键字查询")
    @GetMapping("search/{pageNum}/{pageSize}")
    public ResponseUtil searchByKeyWord(@RequestParam(required = false) String keyword,
                                        @RequestParam(required = false) Integer status, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        return merchantService.searchByKeyWord(keyword, status, pageNum, pageSize);
    }

    //更新商户信息
    @ApiOperation(value = "更新商户信息")
    @PutMapping
    public ResponseUtil update(@RequestBody Merchant merchant) {
        return merchantService.update(merchant);
    }

    /**
     * 根据用户位置查询附近商户
     *
     * @param userLon
     * @param userLat
     * @param radius
     * @param limit
     * @return
     */
    @GetMapping("/wx/nearby")
    public ResponseUtil getNearby(
            @RequestParam Double userLon,
            @RequestParam Double userLat,
            @RequestParam(required = false, defaultValue = "3") Double radius,
            @RequestParam(required = false, defaultValue = "20") Long limit) {

        return merchantService.getNearbyMerchants(userLon, userLat, radius, limit);
    }

    /**
     * 微信小程序获取附近商户
     *
     * @param merchantId
     * @return
     */
    @GetMapping("/wx/detail/{merchantId}")
    public ResponseUtil getDetail(@PathVariable Integer merchantId) {
        return merchantService.getDetail(merchantId);
    }

    /**
     * 地址解析接口（前端调用此接口）
     *
     * @param address 详细地址
     * @return 统一响应结果（包含经纬度）
     */
    @GetMapping("/geocode")
    public ResponseUtil getGeocode(@RequestParam String address) {
        // 校验地址参数
        if (address == null || address.trim().isEmpty()) {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }

        try {
            // 调用服务类获取经纬度
            return merchantService.getLocationByAddress(address.trim());
        } catch (RuntimeException e) {
            // 业务异常（如地址解析失败）
            return ResponseUtil.get(ResponseEnum.FAIL, e.getMessage());
        } catch (Exception e) {
            // 其他异常（如网络错误）
            e.printStackTrace();
            return ResponseUtil.get(ResponseEnum.FAIL, "服务器异常");
        }
    }
}
